<?php 
	class Test_home_works extends CI_Model{

	 	function __construct(){
        	parent::__construct();
    	}

    	/*function get_home_work_list($limit = 0, $offset = 10, $conditions = array()){
            $where = " WHERE 1=1";
            if(!empty($conditions['subject_id'])){
                $where .= " AND tv.subject_id = '{$conditions['subject_id']}'";
            }
            if(!empty($conditions['home_work_name'])){
                $where .= ' AND tv.name like "%'.$conditions['home_work_name'].'%"';
            }
            $sql = "SELECT SQL_CALC_FOUND_ROWS tv.*, t.teacher_name, s.title FROM {$this->db->dbprefix}tk_test_home_works AS tv ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}b_subject AS s ON(s.id=tv.subject_id) ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}o2o_teachers AS t ON(t.id=tv.teacher_id) ";
            $sql .= "$where ORDER BY id DESC LIMIT $limit, $offset ";
            $result = $this->db->query($sql)->result_array();
            $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
            return array("count" => $row_num['row_num'], "result" => $result);
        }*/

        function get_home_work_list($limit = 0, $offset = 10, $conditions = array()){
            $where = " WHERE tvp.type = 3 AND tvp.school_id = '{$this->user_data['school_id']}'";
            if(!empty($conditions['subject_id'])){
                $where .= " AND tv.subject_id = '{$conditions['subject_id']}'";
            }
            if(!empty($conditions['home_work_name'])){
                $where .= ' AND tv.name like "%'.$conditions['home_work_name'].'%"';
            }
            $sql = "SELECT SQL_CALC_FOUND_ROWS tvp.name AS pname, tvp.start_time, tvp.end_time, tv.*, t.teacher_name, s.title FROM {$this->db->dbprefix}tk_test_volumes_publish AS tvp ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}tk_test_volumes AS tv ON(tv.id=tvp.volume_id)";
            $sql .= "LEFT JOIN {$this->db->dbprefix}b_subject AS s ON(s.id=tv.subject_id) ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}o2o_teachers AS t ON(t.id=tvp.teacher_id) ";
            $sql .= "$where ORDER BY id DESC LIMIT $limit, $offset ";
            $result = $this->db->query($sql)->result_array();
            $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
            return array("count" => $row_num['row_num'], "result" => $result);
        }

        function get_home_work_history($limit = 0, $offset = 0, $conditions = array()){
            $where = " WHERE 1=1";
            if(!empty($conditions['home_work_id'])){
                $where .= " AND home_work_h.home_work_id = '{$conditions['home_work_id']}'";
            }

            if(!empty($conditions['name'])){
                $where .= " AND s.name like '%{$conditions['name']}%'";
            }
            
            $sql = "SELECT SQL_CALC_FOUND_ROWS home_work_h.*, s.name FROM {$this->db->dbprefix}tk_home_work_historys AS home_work_h ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}o2o_student AS s ON(s.user_id=home_work_h.uid) ";
            $sql .= "$where ORDER BY id DESC LIMIT $limit, $offset ";
            $result = $this->db->query($sql)->result_array();
            $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
            return array("count" => $row_num['row_num'], "result" => $result);
        }
    }